package com.addzero.web.ui.hooks.list

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.addzero.common.anno.Good
import com.addzero.web.ui.hooks.UseHook


/**
 * 高级抽象的LazyColumn组件
 * @param T 数据类型
 * @param items 数据列表
 * @param getIdFun 数据ID映射函数，用于生成唯一key
 * @param itemContent 项目内容渲染函数
 */
@Good
class UseList<T>(
    val initList: List<T> = emptyList<T>(),
    val getIdFun: (T) -> Any = { it.hashCode() },
    val itemContent: @Composable (T) -> Unit,
) : UseHook<UseList<T>> {
    //    private val items: List<T>,
    var items by mutableStateOf(initList)

    override val render: @Composable (() -> Unit)
        get() = {
            LazyColumn(
                modifier = modifier
            ) {
                items(items = items, key = { getIdFun(it) }) { item ->
                    itemContent(item)
                }
            }

        }


}

